//
//  WLstatusOriginalView.swift
//  WLWeibo
//
//  Created by Devil on 16/8/13.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit
import SnapKit
class WLstatusOriginalView: UIView {

    //原创微博的底部约束记录
    var originginalViewBottomConstraint: Constraint?
    //定义一个ViewModel 供外界使用
    var statusViewModel: WLStatusViewModel?{
    
        didSet{
        
            headImageView.wl_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.text = statusViewModel?.sinaSourcceStr
            
            contentLabel.text = statusViewModel?.statusModel?.text
            membershipImageView.image = statusViewModel?.mbrankImage
            avatarImageView.image = statusViewModel?.verifiedImage
            
            originginalViewBottomConstraint?.uninstall()
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {
                pictureView.pictureInfos = statusViewModel?.statusModel?.pic_urls

                self.snp_makeConstraints(closure: { (make) in
                    originginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                pictureView.hidden = false
            }else{
            
                self.snp_makeConstraints(closure: { (make) in
                    originginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
            
            }
        
        }
    
    }
    
    func dealSinaTimeStr(createAt: String) -> String{
        let df = NSDateFormatter()
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        df.locale = NSLocale(localeIdentifier: "en_US")
        let sinaDate = df.dateFromString(createAt)!
        
        let isThisYear = sinaIsThisYear(sinaDate)
        if isThisYear {
            let calender = NSCalendar.currentCalendar()
            
            if calender.isDateInToday(sinaDate) {
                let S = Int(NSDate().timeIntervalSinceDate(sinaDate))
                printLog(S)
                
                if S < 60  {
                    return "刚刚"
                }else if S >= 60 && S < 3600{
                
                    return "\(S/60)分钟前"
                
                }else{
                
                    return "\(S/3600)小时前"
                
                }
            }else if calender.isDateInYesterday(sinaDate){
                df.dateFormat = "昨天 HH:mm"
            }else{
            
                df.dateFormat = "MM月dd日 HH:mm"
            }
        }else{
        
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
        
        }
        return df.stringFromDate(sinaDate)
        
     }
    
    func sinaIsThisYear(sinaDate: NSDate) -> Bool{
        let df = NSDateFormatter()
        df.dateFormat = "yyyy"
        df.locale = NSLocale(localeIdentifier: "en_US")
        let sinaTimeStr = df.stringFromDate(sinaDate)
        let currentTimeStr = df.stringFromDate(NSDate())
        return sinaTimeStr == currentTimeStr
      }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
        backgroundColor = RandomColor()
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp_makeConstraints {(make)-> Void in
        
            make.leading.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - 250)/2)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) in
            originginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
     
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    private lazy var nameLabel: UILabel = {
    
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        return lab
    }()
    
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    private lazy var timeLabel: UILabel = UILabel(textColor: AppThemeColor,textSize: 10)
    
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(),textSize: 10)
     private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(),textSize:15,maxWidth: SCREENW - 20)
    
    private lazy var pictureView: WLStatusPictureView = WLStatusPictureView()
    
    

}
